package com.evil.common.application.enums.order;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * 订单搜索类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum OrderSearchTypeEnum {

    ALL("", "全部"),
    BLANK("blank", "未填写"),
    UN_BLANK("unBlank", "已填写"),
    ;

    @Getter
    private final String type;
    @Getter
    private final String name;

    public static Optional<OrderSearchTypeEnum> findByType(String type) {
        if (StringUtils.isBlank(type)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type.equals(type)).findFirst();
    }

    public static OrderSearchTypeEnum isExistByType(String type) {
        return OrderSearchTypeEnum.findByType(type).orElseThrow(OrderSearchTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "订单搜索类型参数错误");
    }
}
